package com.woniu.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.entity.PageResult;
import com.woniu.entity.Result;
import com.woniu.pojo.Goods;
import com.woniu.pojo.GoodsType;
import com.woniu.pojo.Imgs;
import com.woniu.service.IGoodsService;
import com.woniu.vo.GoodsEditVo;
import com.woniu.vo.GoodsVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author tanMax
 * @since 2022-10-12
 */
@RestController
@RequestMapping("/goods")
@Api(tags = "商品管理")
public class GoodsController extends BaseController{
    @Autowired
    private IGoodsService goodsService;

    @PostMapping("/search/{currentPage}/{pageSize}")
    @ApiOperation("分页搜索查询")
    public Result search(@RequestBody GoodsVo goodsVo, @PathVariable Integer currentPage, @PathVariable Integer pageSize){
        PageResult list = goodsService.search(goodsVo,currentPage,pageSize);
        return toDataResult(list);
    }
    @GetMapping("/type")
    @ApiOperation("获取类型")
    public Result type(){
        GoodsType type = new GoodsType();
        type.setOne("虚拟物品");
        type.setTwo("实物物品");
        return toDataResult(type);
    }
    @PostMapping("/add/{dynamicTags}")
    @ApiOperation("添加商品")
    public Result add(@RequestBody Goods goods,@PathVariable String[] dynamicTags){
        if (dynamicTags.length>0) {
            String label="";
            for (String dynamicTag : dynamicTags) {
                label=label+dynamicTag+",";
            }
            String labels=label.substring(0,label.length()-1);
            goods.setLabel(labels);
        }
        boolean save = goodsService.save(goods);
        String id = goods.getId();
        return toDataResult(id);
    }
    @GetMapping("/{id}")
    @ApiOperation("通过商品id获取商品")
    public Result selectById(@PathVariable String id){
        GoodsEditVo goodsEditVo = new GoodsEditVo();
        List<Imgs> list1 = goodsService.getImages(id);
        List<String> list = new ArrayList<>();
        if (list1!=null){
            for (Imgs imgs : list1) {
                if (imgs.getIsOne().equals("0")){
                    goodsEditVo.setImg(imgs.getImg());
                }
                    list.add(imgs.getImg());
            }
        }
        Goods goods = goodsService.getById(id);
        String label="";
        label = goods.getLabel();
        String[] split = label.split(",");
        goodsEditVo.setGoods(goods);
        goodsEditVo.setDynamicTags(split);
        goodsEditVo.setImageList(list);
        return toDataResult(goodsEditVo);
    }
    @PutMapping("/edit/{dynamicTags}")
    @ApiOperation("修改商品")
    public Result edit(@RequestBody Goods goods,@PathVariable String[] dynamicTags){
        if (dynamicTags.length>0) {
            String label="";
            for (String dynamicTag : dynamicTags) {
                label=label+dynamicTag+",";
            }
            String labels=label.substring(0,label.length()-1);
            goods.setLabel(labels);
        }
        boolean save = goodsService.updateById(goods);
        return toResult(save);
    }
    @DeleteMapping("/{id}")
    @ApiOperation("删除商品")
    public Result deleteById(@PathVariable String id){
        Integer b = goodsService.deleteById(id);
        return toResult(b);
    }

    @PostMapping("/all/{currentPage}/{pageSize}")
    @ApiOperation("分页获取全部商品-用户端")
    public Result selectAll(@PathVariable Integer currentPage, @PathVariable Integer pageSize){
        Page<Goods> page = new Page<>(currentPage,pageSize);
        Page<Goods> goodsPage = goodsService.page(page);
        return toDataResult(new PageResult(goodsPage.getTotal(),goodsPage.getRecords()));
    }

    @PostMapping("/bySortId/{sortId}/{currentPage}/{pageSize}")
    @ApiOperation("分页查询分类下的商品-用户端")
    public Result bySortId(@PathVariable String sortId,@PathVariable Integer currentPage,@PathVariable Integer pageSize){
        PageResult list = goodsService.selectBySortId(sortId,currentPage,pageSize);
        return toDataResult(list);
    }
}
